#ifndef CONCEPT_COLLECTION_JS
#define CONCEPT_COLLECTION_JS

#include "Concept.js"

/* class */ ConceptCollection = function(/* string */ input)
{
	var /* Array */ _concepts = new Array();
	
	(function()
	{
		var currentIndex = 0;
	
		var /* string */ NextHead = function()
		{
			// Starting FROM the first character or FROM the first character after a right-brace or a semi-colon.
			var head = "";
			var hasSpaceChar = true;
		
			while (currentIndex < input.length)
			{
				var char = input[currentIndex];
			
				if (char == '{' || char == ';')
				{
					if(head.length == 0)
					{
						throw "Head is void.";
					}
			
					return head;
				}
				else if (char == '}')
				{
					throw "There is too much right-braces.";
				}
				else if (char == ' ' || char == '\t' || char == '\n')
				{
					hasSpaceChar = true;
				}
				else
				{
					if (hasSpaceChar)
					{
						if (head.length != 0)
						{
							head += ' ';
						}
					
						hasSpaceChar = false;
					}
				
					head += char;
				}
			
				// Stopping AT a left-brace or a semi-colon.
				currentIndex += 1;
			}
		
			if (head.length != 0)
			{
				throw "There is text after the last right-brace: \"" + head + "\"";
			}
		
			return null;
		};
	
		var NextBody = function()
		{
			// Starting FROM a left-brace or a semi-colon.
			var char = input[currentIndex];
			currentIndex += 1;
			if (char == ';')
			{
				return null;
			}
		
			// It's a left-brace.
			var braceDepth = 1;
			var body = "";
		
			while (currentIndex < input.length)
			{
				var char = input[currentIndex];
				currentIndex += 1;
			
				if (char == '{')
				{
					braceDepth += 1;
				}
				else if (char == '}')
				{
					braceDepth -= 1;
				
					if(braceDepth == 0)
					{
						return body;
					}
				}
			
				body += char;
			}
		
			throw "Missing right-brace.";
		};
	
		for (var head = NextHead(); head != null; head = NextHead())
		{
			_concepts.push(new Concept(head, NextBody()));
		}
	})();
	
	/* Concept */ this.At = function(/* int */ index)
	{
		if (index < 0 || index >= _concepts.length)
		{
			throw "ArgumentOutOfRangeException";
		}
		
		return _concepts[index];
	};
	
	/* int Length { get; } */
	this.__defineGetter__("Length", function() { return _concepts.length; });
	this.__defineSetter__("Length", function() { throw "MethodAccessibilityException"; });
};

#endif
